import { _decorator, Component, find, instantiate, Label, Node, Prefab, randomRange, v2, v3 } from 'cc';
import { ModuleBase } from '../ModuleBase';
import Singleton from '../../FrameWork/Common/Singleton';
import { ResMgr } from '../../FrameWork/Res/ResMgr';
const { ccclass, property } = _decorator;

@ccclass('SunMgr')
export class SunMgr extends ModuleBase {

    // 总的阳光数
    private _totalSun: number = 0;

    get totalSun() {
        return this._totalSun;
    }

    set totalSun(value: number) {
        this._totalSun = value;
        this.updateSunLabel();
    }

    private _sunNumLabel: Label = null;

    onInit(): void {
        this._totalSun = 50;
        this._sunNumLabel = this.node.parent.getChildByName("ChooserBg").getChildByName("SunNum").getComponent(Label);
        this.schedule(() => {
            this.createSun();
        }, 3);
    }

    addSun(sunNum: number) {
        this.totalSun += sunNum;
    }

    reduceSun(sunNum: number) {
        if (this._totalSun >= sunNum) {
            this.totalSun -= sunNum;
            return true;
        }
        return false;
    }

    // 检查是否有足够的阳光
    isEnoughSun(sunNum: number) {
        return this._totalSun >= sunNum;
    }

    createSun() {
        let sunPrefab = ResMgr.instance.getRes("Game", "Sun") as Prefab;
        let sunNode = instantiate(sunPrefab);
        sunNode.parent = this.node;
        // 随机位置生成阳光
        let randomX = randomRange(-960, 960);
        sunNode.setPosition(v3(randomX, 540, 0));
    }

    updateSunLabel() {
        this._sunNumLabel.string = this._totalSun.toString();
    }
}